package com.zqh.myfmmall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-01-19 21:03
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig{
    /*swagger会帮助我们⽣成接⼝⽂档
     * 1：配置⽣成的⽂档信息
     * 2: 配置⽣成规则*/
    /*Docket封装接⼝⽂档信息*/
    @Bean
    public Docket getDocket() {
        //配置全局响应状态码
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseMessageBuilder().code(200).message("操作成功").build());
        responseMessageList.add(new ResponseMessageBuilder().code(400).message("操作失败").build());
        responseMessageList.add(new ResponseMessageBuilder().code(401).message("权限不足").build());
        responseMessageList.add(new ResponseMessageBuilder().code(404).message("接口不存在").build());
        responseMessageList.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").build());
        responseMessageList.add(new ResponseMessageBuilder().code(600).message("请重新登录").build());

        //创建封⾯信息对象
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder.title("《锋迷商城》后端接⼝说明")
                .description("此⽂档详细说明了锋迷商城项⽬后端接⼝规范....")
                .version("v 2.0.1")
                .contact(new Contact("KONO曾淇杭哒！", "no", "zqh17689657694.com"));
        ApiInfo apiInfo = apiInfoBuilder.build();
        //指定⽣成的⽂档中的封⾯信息：⽂档标题、版本、作者
        Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo)
                //配置全局响应状态码
                .globalResponseMessage(RequestMethod.GET,responseMessageList)
                .globalResponseMessage(RequestMethod.POST,responseMessageList)
                .globalResponseMessage(RequestMethod.PUT,responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE,responseMessageList)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zqh.myfmmall.controller"))
                .paths(PathSelectors.any())
                .build();

        return docket;
    }
}
